
#ifndef __CAMERA_H__
#define __CAMERA_H__

#include "rt.hh"

class camera {
	public : 
		point3 origin;
		vec3 horizontal;
		vec3 vertical;
		point3 lower_left_corner;

		vec3 x, y, z;
		double lens_radius;

	public : 
		camera(point3 lookfrom, point3 lookat, vec3 vup, double vfov, double win_ratio, double aperture, double focus_dist) {
			auto h = std::tan(degree_to_radian(vfov) / 2.0);

			auto viewport_height = 2.0 * h;
			auto viewport_width = win_ratio * viewport_height;
			
			z = unit_vec(lookfrom - lookat);
			y = unit_vec(cross(vup, z));
			x = cross(z, y);

			origin = lookfrom;
			horizontal = focus_dist * viewport_width * y;
			vertical = focus_dist * viewport_height * x;
			lower_left_corner = origin - horizontal / 2 - vertical / 2 - focus_dist * z;

			lens_radius = aperture / 2.0;
		}

		ray get_ray(double u, double v) const { 
			vec3 rd = lens_radius * rand_in_unit_disk();
			vec3 offset = x * rd.x() + y * rd.y();

			return ray{origin + offset,  lower_left_corner + v * vertical + u * horizontal - origin - offset};
		}
};

#endif
